package com.quxing.amazon.security;

import com.quxing.amazon.model.dto.UserBasicInfo;
import com.quxing.amazon.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author zhb
 * @description
 * @date 2020/11/27 15:17
 */
@Service
@RequiredArgsConstructor
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    private final UserService userService;

    @Override
    @Cacheable(cacheNames = "jwtUser", key = "#username")
    public JwtUser loadUserByUsername(String username) throws UsernameNotFoundException {
        UserBasicInfo userBasicInfo = userService.selectUserBasicInfoByUsername(username);
        if (userBasicInfo == null) {
            throw new UsernameNotFoundException("该用户不存在！");
        }
        return JwtUser.create(userBasicInfo);
    }
}
